package diamondRingDao.impl;

import diamondRingDao.UserDao;
import diamondRingEntity.User;
import diamondRingEntity.DiamondRingUser;
import tool.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl extends BaseDao implements UserDao {

    @Override
    public int Login(User user) {
        ResultSet rs=null;
        int count=0;
        try {
            String sql="select count(*) from users where loginPwd=? and userEmail=?";
            Object params[]={user.getEmail(), user.getPwd()};
            rs=super.executeQuery(sql,params);
            if (rs.next()){
                //拿到查询结果
                count=rs.getInt(1);
            }
        } catch (SQLException e) {
            System.err.println("数据访问层"+e);
        }
        return count;
    }

    @Override
    public int Regist(User user) {
        int count=0;
        try {
            String sql="select count(*) from users where userEmail=?";
            Object[] params={user.getEmail()};
            rs=super.executeQuery(sql,params);
            if (rs.next()){
                count = rs.getInt(1);
            }
        } catch (Exception e) {
            System.err.println("数据访问层出错"+e);
        }
        return count;
    }

    @Override
    public int isRegist(User user) {
        int count=0;
        try {
            String insertSQL="INSERT into users(loginName,loginPwd,userEmail) VALUES(?,?,?)";
            Object[] insertParams={user.getName(), user.getPwd(), user.getEmail()};
            count=super.executeUpdate(insertSQL,insertParams);
        } catch (Exception e) {
            System.err.println("数据访问层出错"+e);
        }
        return count;
    }

    @Override
    public DiamondRingUser findUser(String email, int pwd) {
        ResultSet rs=null;
        DiamondRingUser user=null;
        try {
            String sql="SELECT * FROM users WHERE users.userEmail=? and users.loginPwd=?";
            Object[] param={email,pwd};
            rs=this.executeQuery(sql,param);
            while (rs.next()){
                user=new DiamondRingUser();
                user.setUserId(rs.getInt(1));
                user.setLoginName(rs.getString(2));
                user.setLoginPwd(rs.getString(3));
                user.setUserSex(rs.getString(4));
                user.setUserName(rs.getString(5));
                user.setUserQQ(rs.getString(6));
                user.setUserPhone(rs.getString(7));
                user.setUserEmail(rs.getString(8));
                user.setCreateTime(rs.getString(9));
                user.setLastTime(rs.getString(10));
                user.setPayPwd(rs.getString(11));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    @Override
    public int userUpdatePwd(int userid,int oldPwd, int newPwd) {
        int countPwd=0;
        int count=0;
        ResultSet rs=null;
        try {
            String sqlSele="SELECT users.loginPwd\n" +
                    "FROM users WHERE users.userId=?";
            Object[] param={userid};
            rs=this.executeQuery(sqlSele,param);
            while (rs.next()){
                countPwd=rs.getInt(1);
            }
            if (countPwd==oldPwd){
                String sqlUpdate="UPDATE users SET users.loginPwd=? WHERE users.userId=?";
                Object[] param2={newPwd,userid};
                count=this.executeUpdate(sqlUpdate,param2);
            }else {
                count=2;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }
}
